package corrida.array;

/**
 * 
 * @author acml
 * 
 */

public class ThreadArray implements Runnable {

	private String[] arrayStr = new String[10];

	public ThreadArray() {

		arrayStr[1] = "pedro";
		arrayStr[2] = "kelyson";
		arrayStr[0] = "alan";
		arrayStr[3] = "leo";
		arrayStr[4] = "thiago";
		arrayStr[5] = "bruno";
		arrayStr[6] = "thays";
		arrayStr[7] = "panta";
		arrayStr[8] = "romulo";
		arrayStr[9] = "gaucho";
	}

	@Override
	public void run() {

		String[] array = quick_sort(arrayStr, 1 ,10);
		//String[] array = insertSort();
		//String[] array = bubbleSort();

		for (int i = 0; i < array.length; i++) {
			String str = array[i];

			System.out.println(i + 1 + " - " + str);
		}

	}

	private String[] insertSort() {

		int i, j;

		for (i = 1; i < arrayStr.length; i++) {
			String temp = arrayStr[i];
			j = i;
			while (j > 0 && arrayStr[j - 1].compareToIgnoreCase(temp) > 1) {
				arrayStr[j] = arrayStr[j - 1];
				--j;
			}
			arrayStr[j] = temp;
		}

		return arrayStr;
	}

	private String[] bubbleSort() {

		int i, j;
		String temp = "";

		for (i = 0; i < arrayStr.length; i++) {
			for (j = 0; j < i; j++) {
				if ((arrayStr[i].compareToIgnoreCase(arrayStr[j]) < 0)) {
					temp = arrayStr[i];
					arrayStr[i] = arrayStr[j];
					arrayStr[j] = temp;
				}
			}
		}

		return arrayStr;
	}

	public String[] quick_sort(String[] v, int ini, int fim) {
		
		int meio;

		if (ini < fim) {
			meio = partition(v, ini, fim);
			quick_sort(v, ini, meio);
			quick_sort(v, meio + 1, fim);
		}
		
		return v;
	}

	public int partition(String[] v, int ini, int fim) {
		
		String pivo;
		int topo, i;
		pivo = v[ini];
		topo = ini;

		for (i = ini + 1; i < fim; i++) {
			if (v[i].compareToIgnoreCase(pivo) < 0) {
				v[topo] = v[i];
				v[i] = v[topo + 1];
				topo++;
			}
		}
		v[topo] = pivo;
		
		return topo;
	}

}
